package com.keste.ojt1.view.task10.cpalaparthi.bean;

import java.io.BufferedReader;

import java.io.InputStreamReader;
import java.io.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import javax.el.ExpressionFactory;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.view.rich.component.rich.input.RichInputFile;

import oracle.binding.BindingContainer;

import oracle.binding.OperationBinding;

import org.apache.myfaces.trinidad.model.UploadedFile;

import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.view.rich.component.rich.input.RichInputFile;

import oracle.binding.BindingContainer;

import oracle.binding.OperationBinding;

import org.apache.myfaces.trinidad.model.UploadedFile;

public class FileProcessor {
  private RichInputFile upLoadFile;
  private BindingContainer bindings = null;

  public FileProcessor() {
  }
  private BindingContainer getBindings() {
      
          if (this.bindings == null) {
              this.bindings = (BindingContainer)getDCBindingContainer();
          }
          return bindings;
      }
     
   /**
    * generic method to get handle to DCBindingContainer.
    * @return DCBindingContainer handle
    */
   public DCBindingContainer getDCBindingContainer() {
      
       FacesContext fctx = FacesContext.getCurrentInstance();
       ExpressionFactory expFactory =
           FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
       DCBindingContainer bc =
           (DCBindingContainer)expFactory.createValueExpression(fctx.getELContext(),
                                                                "#{bindings}",
                                                                DCBindingContainer.class).getValue(fctx.getELContext());

       return bc;
   }

  public void fileUploadValueChange(ValueChangeEvent valueChangeEvent) {
    // Add event code here...
    UploadedFile upFile = (UploadedFile)valueChangeEvent.getNewValue();
          
          
          List list=new ArrayList();
        

          try{
            BufferedReader reader =
                            new BufferedReader(new InputStreamReader(upFile.getInputStream()));  
              
              for(int i=0; i<7; i++){
            String line = reader.readLine();
              StringTokenizer stok = new StringTokenizer(line, ",");
             List list1= new ArrayList();
              while(stok.hasMoreTokens()){
               
               list1.add(stok.nextToken());
              }       
                  list.add(list1);
           }
            System.out.println("Token2: " +list);
            BindingContainer bindings = getBindings();
                    OperationBinding op = bindings.getOperationBinding("setCSVDatatoVO");
              op.getParamsMap().put("list", list);
                    if (op != null) {
                        op.execute();
                    }
          }catch(Exception e){
            System.out.println("Trace: "+ e);
          }
        }
      
  

  public void setUpLoadFile(RichInputFile upLoadFile) {
    this.upLoadFile = upLoadFile;
  }

  public RichInputFile getUpLoadFile() {
    return upLoadFile;
  }
  public void validateData(ActionEvent actionEvent) {
          // Add event code here...
      }
      
    public void mfgDateValidator(FacesContext facesContext, 
                                     UIComponent uiComponent, Object object) {
            // Add event code here...
    }
    public boolean expDateValidator(FacesContext facesContext, 
                                     UIComponent uiComponent, Object object) {
            Date mfgDate = (Date)uiComponent.getAttributes().get("mfgDataValue");
            Date expDate = (Date)object;
         /*   int year = mfgDate.getYear();
            int month = mfgDate.getMonth();
            int day=mfgDate.getDay();
            int year1 = expDate.getYear();
            int month1 = expDate.getMonth();
            int day1=expDate.getDay();
            if(year < year1)
            {
                if(month < month1) {
                    if(day < day1) {
                
                } */
            if(mfgDate.compareTo(expDate) > -1){
                     FacesContext fctx = FacesContext.getCurrentInstance(); 
                     fctx.addMessage("VacationQueryComponent", new FacesMessage(FacesMessage.SEVERITY_ERROR, "From mfgdate cannot be lower than To ExpDate","From MfgDate cannot be lower than To ExpDate"));
                  return false;
                 }
            else
            {    
               
         return true;
            }
                                     }}